package com.example.myapplication;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;

/**
 * 属性动画
 * Created by Lx on 2018/12/19.
 */

public class ObjectAnimatorActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView imageView;

    TextView textView;

    Button translate, scale, rotate, alpha;

    Button start;
    //数字格式化
    DecimalFormat format = new DecimalFormat("#.00");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_objectanimator);
        init();
    }

    private void init() {
        textView = findViewById(R.id.obj_tv);
        imageView = findViewById(R.id.obj_img);
        translate = findViewById(R.id.obj_btn1);
        scale = findViewById(R.id.obj_btn2);
        rotate = findViewById(R.id.obj_btn3);
        alpha = findViewById(R.id.obj_btn4);
        start = findViewById(R.id.obj_btn5);

        translate.setOnClickListener(this);
        scale.setOnClickListener(this);
        rotate.setOnClickListener(this);
        alpha.setOnClickListener(this);
        start.setOnClickListener(this);
    }
//    setInterpolator()：设置动画插值
//    控制动画的变化速率，系统中定义了好几种Interpolator:
//    LinearInterpolator--均匀的速率改变
//    AccelerateDecelerateInterpolator--先加速后减速
//    AccelerateInterpolator--加速
//    DecelerateInterpolator--减速
//    CycleInterpolator--动画循环播放特定的次数，速率改变沿着正弦曲线
//    setDuration()：设置动画执行时间，动画时间以毫秒为单位（ms）
//    setRepeatCount()：设置动画重复次数
//    大于0的值就代表重复几次，如果需要无限循环，设为-1，上面的Animation.INFINITE是系统给的常量，值为-1，代表无限循环，我们建议使用这个常量，如果设为0呢？也是执行一次。
//    setRepeatMode()：设置动画重复模式
//    setStartDelay():设置动画延时操作，也是以毫秒为单位（ms）
//    setTarget():设置动画的对象

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.obj_btn1:
                //平移
                ObjectAnimator translation = ObjectAnimator.ofFloat(imageView, "translationX", 0.0f, 350.0f, 0.0f);
                translation.setInterpolator(new AccelerateInterpolator());
                translation.setDuration(2000);
                translation.setRepeatCount(Animation.RELATIVE_TO_SELF);
                translation.setRepeatMode(Animation.RELATIVE_TO_SELF);
                translation.start();
                break;
            case R.id.obj_btn2:
                //缩放
                ObjectAnimator scale = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 1.5f,1.0f);
                scale.setDuration(2000);
                scale.setRepeatCount(Animation.RESTART);
                scale.setRepeatMode(Animation.RESTART);
                scale.start();

                break;
            case R.id.obj_btn3:
                //旋转
                ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotationX", 0.0f, 90.0f, 0.0F);
                rotation.setDuration(2000);
                rotation.setRepeatCount(Animation.RESTART);
                rotation.setRepeatMode(Animation.RESTART);
                rotation.start();

                break;

            case R.id.obj_btn4:
                //透明度
                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.3f, 1.0F, 0.3f);
                alpha.setDuration(2000);
                alpha.setRepeatCount(Animation.RESTART);
                alpha.setRepeatMode(Animation.RESTART);
                alpha.start();

                break;
            case R.id.obj_btn5:
                //文字效果
                ValueAnimator anim = ValueAnimator.ofFloat(0, 2000);
                anim.setDuration(2000);
                anim.setInterpolator(new LinearInterpolator());
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value = (float) valueAnimator.getAnimatedValue();
                        textView.setText(format.format(value));
                    }
                });
                anim.start();
                //改变背景颜色
                ObjectAnimator objectAnimator = ObjectAnimator.ofArgb(textView, "backgroundColor", getResourcesColor(R.color.colorAccent), getResourcesColor(R.color.colorPrimaryDark));
                //设置无限循环
                objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
                //设置反转效果
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.setDuration(2000);
                objectAnimator.start();

                break;
        }
    }

    /**
     * 获取资源中的颜色
     *
     * @param color
     * @return
     */
    public int getResourcesColor(int color) {
        int ret = 0x00ffffff;
        try {
            ret = getResources().getColor(color);
        } catch (Exception e) {
        }
        return ret;
    }

}